package com.chenss.dragonserapp.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenss.dragonserapp.R;
import com.chenss.dragonserapp.app.AppInfo;
import com.chenss.dragonserapp.app.Constant;
import com.chenss.dragonserapp.model.RecordListModel;
import com.chenss.dragonserapp.network.NetworkRequest;
import com.chenss.dragonserapp.network.RequestCallbcak;
import com.chenss.dragonserapp.response.RecordListResponse;
import com.chenss.dragonserapp.utils.GsonUtil;
import com.chenss.dragonserapp.utils.QRCodeUtil;
import com.chenss.dragonserapp.base.BaseActivity;
import com.chenss.dragonserapp.utils.ToastUtil;

import org.json.JSONException;

import java.util.List;
import java.util.logging.Logger;

/**
 * PageName: 收款二维码
 * Create by chenss at 2018/4/20 2:54
 */
public class QrPicActivity extends BaseActivity implements RequestCallbcak {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    TextView tv_price;
    ImageView img_qr;
    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装
    String price = "";
    String onlyCode = "";
boolean isFinished=false;
    @Override
    public void initParms(Bundle parms) {
        price = getIntent().getStringExtra("price");
        isFinished=false;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_qrimg;
    }

    @Override
    public void initView(View view) {
        ll_left = findView(R.id.ll_left);//左边按钮
        tv_page_title = findView(R.id.tv_page_title);
        tv_price = findView(R.id.tv_price);
        img_qr = findView(R.id.img_qr);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("收款");


    }

    @Override
    public void doBusiness(Context mContext) {
        tv_price.setText("收款金额：" + price + "元");
        onlyCode = System.currentTimeMillis() + "";
        String qrString = onlyCode + "," + AppInfo.getUser(this).getStore_id() + "," + price;
        Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(qrString, 600, 600);
        img_qr.setImageBitmap(mBitmap);
        networkRequest = new NetworkRequest(mContext, this);
        getOrder_an_Paystate();
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ll_left:
                finish();
                break;
        }
    }

    /**
     * 直接下单扫码支付获取支付状态
     */
    private void getOrder_an_Paystate() {
        /*
           'store_id' => '7',
            'only_code' => '1504687174038',
         */
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Order, Constant.classes.Order_an_Paystate);
        parmMap.put("store_id", AppInfo.getUser(this).getStore_id());//服务站用户id
        parmMap.put("only_code", onlyCode);
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, false, Constant.classes.Order_an_Paystate);
    }

    @Override
    public void onFaild(int orderCode, String result) {
//        ToastUtil.showShort(this, result);
        if (orderCode == 1&&!isFinished) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getOrder_an_Paystate();
                }
            }, 5000);
        }
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
//            RecordListResponse recordListResponse = GsonUtil.GsonToBean(result,RecordListResponse.class);
//            if (null != recordListResponse) {
//                if (0 == recordListResponse.getStatus()) {
//                    if(freshState==REFRESHING){
//                        list.clear();
//                    }
//                    List<RecordListModel> models=recordListResponse.getData();
//                    if(null!=models&&models.size()>0){
//                        list.addAll(models);
//                        listAdapter.setDataList(list);
//                    }else{
//                        if(freshState==REFRESHING){
//                            listAdapter.setDataList(list);
//                            ToastUtil.showShort(this,"暂无数据");
//                        }else{
//                            ToastUtil.showShort(this,"没有更多数据了");
//                        }
//
//                    }
//
//                }
//            }
            ToastUtil.showShort(QrPicActivity.this, "支付成功");

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isFinished=true;
    }
}
